/**
 * 
 */
package com.cinyi.ormx.android.ui;

import com.cinyi.ormx.android.App;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;

/**
 * @author penghui
 * 
 */
public class BaseAct extends Activity {
	protected App context ;
	
	private int mScreenHeight = 0, mScreenWidth = 0;
	private float density = 1.0f;
	private int densityDpi = 1;
	
	private InputMethodManager imm;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		context = (App) getApplication();
		context.setLastActivity(this);
		
		imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
		
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	/**
	 * 通过类名启动Activity
	 * 
	 * @param pClass
	 */
	protected void openActivity(Class<?> pClass) {
		openActivity(pClass, null);
		this.finish();
	}

	/**
	 * 通过Action启动Activity
	 * 
	 * @param pAction
	 */
	protected void openActivity(String pAction) {
		openActivity(pAction, null);
	}

	/**
	 * 通过类名启动Activity，并且含有Bundle数据
	 * 
	 * @param pClass
	 * @param pBundle
	 */
	protected void openActivity(Class<?> pClass, Bundle pBundle) {
		Intent intent = new Intent(this, pClass);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
	}
	
	/**
	 * 通过Action启动Activity，并且含有Bundle数据
	 * 
	 * @param pAction
	 * @param pBundle
	 */
	protected void openActivity(String pAction, Bundle pBundle) {
		Intent intent = new Intent(pAction);
		if (pBundle != null) {
			intent.putExtras(pBundle);
		}
		startActivity(intent);
	}
	
	/**
	 * 初始计算手机屏幕适配信息
	 */
	protected void _calcScreen() {
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		
		mScreenHeight = metric.heightPixels;
		mScreenWidth = metric.widthPixels;
		density = metric.density;
		densityDpi = metric.densityDpi;
		
	}
	
	/**
	 * 返回全局上下文环境对象
	 * @return
	 */
	public App getContext(){
		return context ;
	}
	
	public int getScreenHeight() {
		return mScreenHeight;
	}

	public int getScreenWidth() {
		return mScreenWidth;
	}

	public float getDensity() {
		return density;
	}

	public int getDensityDpi() {
		return densityDpi;
	}
	
	public void showInputDialog(View v) {
		imm.showSoftInput(v, InputMethodManager.SHOW_FORCED);
	}

	public void hiddenInputDialog(View v) {
		imm.hideSoftInputFromWindow(v.getWindowToken(), 0); // 强制隐藏键盘
	}

	/**
	 * 如果输入法在窗口上已经显示，则隐藏，反之则显示
	 * 
	 * @param v
	 */
	public void switchInputDialog() {
		imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
	}

	public void closeKeyboard(final View v) {
		InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		im.hideSoftInputFromWindow(v.getApplicationWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}

	public void toggleSoftKeyboard() {
		((InputMethodManager) this
				.getSystemService(Context.INPUT_METHOD_SERVICE))
				.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

	}
	
}
